//
//  InfoViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/29.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "InfoViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "AccountTableViewCell.h"
#import "InfoDetailsViewController.h"
#import "YoungAllData.h"
#import "MJRefresh.h"
#import "GMDCircleLoader.h"

@interface InfoViewController ()<UITableViewDelegate,UITableViewDataSource,YoungDelegate,MJRefreshBaseViewDelegate>
{
    UITableView *tableView;
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
}
@property(nonatomic,retain)NSMutableArray *zArrayData;
@end

@implementation InfoViewController
@synthesize zArrayData;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden=YES;
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    navTitle.text = @"信息";
    navRightBtn.hidden = YES;
    YoungAllData *young = [[YoungAllData alloc]init];
    young.delegate = self;
    [young gainInfo:[[NSUserDefaults standardUserDefaults]objectForKey:@"userID"] andPageID:@"1" andPerPage:@"15"];
    
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];

}
#pragma mark - 获取信息列表回调
-(void)info:(NSDictionary *)sign{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    zArrayData = [[NSMutableArray alloc]init];
    [zArrayData arrayByAddingObjectsFromArray: [sign objectForKey:@"data"]];
    //还没有用上接口数据
    [self initUI];
    }else{
        if ([[sign objectForKey:@"reason"] isEqualToString:@"暂无数据"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"暂无信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        } else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        }
    }
}
#pragma mark - 信息列表回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
#pragma mark - 返回
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - initUI
-(void)initUI{
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,navView.bottom, self.view.frame.size.width, self.view.frame.size.height-65-50)];
    tableView.backgroundColor = [UIColor clearColor];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:tableView];
    
    [tableView registerClass:[AccountTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = tableView;
    header.delegate = self;
    _header = header;
    
    // 3.2.上拉加载更多
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = tableView;
    footer.delegate = self;
    _footer = footer;
    
    UIButton *belowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    belowBtn.frame = CGRectMake(0,SCREEN_HEIGHT-(0.15625*SCREEN_WIDTH) , SCREEN_WIDTH, 0.15625*SCREEN_WIDTH);
    [belowBtn setBackgroundImage:[UIImage imageNamed:@"信息_06"] forState:UIControlStateNormal];
    [belowBtn addTarget:self action:@selector(backGo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:belowBtn];
}
-(void)backGo{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [zArrayData count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row <3) {
        cell.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    }
    cell.titleLabel.text = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"Subject"];
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    InfoDetailsViewController *info = [[InfoDetailsViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    info.NoticeID = [[zArrayData objectAtIndex:indexPath.row] objectForKey:@"NoticeID"];
    [self.navigationController pushViewController:info animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- ( void )dealloc

{
    
    [ _header free ];
    
    [ _footer free ];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - 刷新
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    
    // 刷新表格
    [tableView reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
    
}
#pragma mark - 刷新控件的代理方法
#pragma mark 开始进入刷新状态
static int pa = 2;
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    NSLog(@"%@----开始进入刷新状态", refreshView.class);
    NSString *str = [NSString stringWithFormat:@"%@",refreshView.class];
    NSLog(@"___%@",str);
    // 1.添加假数据
    if ([str isEqualToString:@"MJRefreshHeaderView"]) {
        NSLog(@"top");
        
        
        [self doneWithView:refreshView];
        
    }else{
        NSLog(@"down");
        [self doneWithView:refreshView];
        
        
    }
}

#pragma mark 刷新完毕
- (void)refreshViewEndRefreshing:(MJRefreshBaseView *)refreshView
{
    NSLog(@"%@----刷新完毕", refreshView.class);
}

#pragma mark 监听刷新状态的改变
- (void)refreshView:(MJRefreshBaseView *)refreshView stateChange:(MJRefreshState)state
{
    switch (state) {
        case MJRefreshStateNormal:
            NSLog(@"%@----切换到：普通状态", refreshView.class);
            break;
            
        case MJRefreshStatePulling:
            NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
            break;
            
        case MJRefreshStateRefreshing:
            NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
            break;
        default:
            break;
    }
}

@end
